package ia.robot.graphics;

/**
 * Class that operates with bitmap images.
 * 
 * @author Jaime A. Corrales Gonzalez
 */
public class Bitmap {

	private int width;
	private int height;
	private int[] pixels;

	public Bitmap(int width, int height) {
		this.width = width;
		this.height = height;

		this.pixels = new int[width * height];
	}

	/**
	 * Draws a tile onto a bitmap.
	 * 
	 * @param bitmap
	 *            - Source bitmap.
	 * @param xOffs
	 *            - Offset x on the target bitmap.
	 * @param yOffs
	 *            - Offset y on the target bitmap.
	 * @param x0
	 *            - Offset x on the source bitmap.
	 * @param y0
	 *            - Offset y on the source bitmap.
	 * @param w
	 *            - Width of the tile.
	 * @param h
	 *            - Height of the tile.
	 */
	public void draw(Bitmap bitmap, int xOffs, int yOffs, int x0, int y0,
			int w, int h) {
		for (int y = 0; y < h; y++) {
			int yPix = y + yOffs;
			if (yPix < 0 || yPix >= height)
				continue;

			for (int x = 0; x < w; x++) {
				int xPix = x + xOffs;
				if (xPix < 0 || xPix >= width)
					continue;

				int src = bitmap.pixels[(x + x0) + (y + y0) * bitmap.width];

				if ((0xFF000000 & src) != 0x0)
					pixels[xPix + yPix * width] = src;
			}
		}
	}

	/**
	 * Fills a region with a single color.
	 * 
	 * @param x0
	 *            - Starting x coordinate.
	 * @param y0
	 *            - Starting y coordinate.
	 * @param x1
	 *            - Final x coordinate.
	 * @param y1
	 *            - Final y coordinate.
	 * @param col
	 *            - Filling color.
	 */
	public void fill(int x0, int y0, int x1, int y1, int col) {
		for (int y = y0; y < y1; y++) {
			if (y < 0 || y >= height)
				continue;

			for (int x = x0; x < x1; x++) {
				if (x < 0 || x >= width)
					continue;

				pixels[x + y * width] = col;
			}
		}
	}

	/**
	 * Returns the height of the bitmap.
	 * 
	 * @return Height of the bitmap.
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * Returns the number of pixels of the bitmap.
	 * 
	 * @return Number of pixels of the bitmap.
	 */
	public int getNumPixels() {
		return pixels.length;
	}

	/**
	 * Returns a certain pixel from the pixel array.
	 * 
	 * @param pos
	 *            - Index of the pixel array.
	 * @return Requested pixel.
	 */
	public int getPixel(int pos) {
		return pixels[pos];
	}

	/**
	 * Returns the array of pixels of the bitmap.
	 * 
	 * @return Array of pixels of the bitmap.
	 */
	public int[] getPixels() {
		return pixels;
	}

	/**
	 * Returns the width of the bitmap.
	 * 
	 * @return Width of the bitmap.
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * Sets a certain pixel from the pixel array.
	 * 
	 * @param pos
	 *            - Index of the pixel array.
	 * @param pix
	 *            - New value of the pixel.
	 */
	public void setPixel(int pos, int pix) {
		pixels[pos] = pix;
	}
}
